<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\AdminController;
use Auth;
use Illuminate\Support\Facades\DB;
use App\Model\Role;
use App\Model\Menu;

class ChartController extends AdminController
{   
    /**
     * 首页
     * @author yhs 2019.07.29
     * @return list
     */
    public function chart(Request $request){
        $list = Role::select('id','name','desc')->get();
        return $list;
    }
    /**
     * 角色修改页面
     * @author yhs 2019.07.29
     * @return first
     */
    public function roleedit(Request $request){
        $first = Role::where('id',$request->id)->first();
        return $first;
    }
    /**
     * 角色添加和修改
     * @author yhs 2019.07.29
     * @return back
     */
    public function roleadd(Request $request){
        if(empty($request->name)){
            $back['message'] = "角色名称不能为空！";
            $back['status'] = 0;
            return $back;
        }
        $data = [
            'name' => $request->name,
            'desc' => $request->desc,
        ];
        try{
            if(empty($request->id)){
                $data['create_time'] = time();
                Role::insert($data);
                $back['message'] = "添加成功！";
                $back['status'] = 1;
            }else{
                Role::where('id',$request->id)->update($data);
                $back['message'] = "修改成功！";
                $back['status'] = 1;
            }
        }catch(\Execption $e){
            $back['message'] = $e->getMessage();
            $back['status'] = 0;
        }
        return $back;
    }
    /**
     * 角色删除
     * @author yhs 2019.07.29
     * @return back
     */
    public function roledel(Request $request){
        if(!empty($request->id)){
            if(Role::where('id',$request->id)->delete()){
                $back['message'] = "删除成功！";
                $back['status'] = 1;
            }else{
                $back['message'] = "删除失败！";
                $back['status'] = 0;
            }
        }else{
            $back['message'] = "请选择角色！";
            $back['status'] = 0;
        }
        return $back;
    }
     /**
     * 选择权限列表
     * @author yhs 2019.07.31
     * @return back
     */
    public function menuPower(Request $request){

        $menu = Menu::select('id','menu_name','eg_name')->where('valid',1)->where('parent_id',0)->get();
        foreach($menu as $k=>$val){
            $submenu = Menu::select('id','menu_name','eg_name','parent_id')->where('valid',1)->where('parent_id',$val->id)->get();
            $val['submenu'] = $submenu;
        }
        $role = Role::where('id',$request->roleid)->first();
        $roleid = explode(',',$role->roleids);//dd($roleids);die;
        foreach($roleid as $key => $val){
            $roleids[] = intval($val);
        }
        $data = [
            'roleids' => $roleids,
            'menu' => $menu,
        ];
        return $data; 
    }
     /**
     * 角色权限
     * @author yhs 2019.08.01
     * @return back
     */
    public function editPower(Request $request){
        if(Role::where('id',$request->roleid)->update(['roleids'=>$request->ids])){
            $back['message'] = "修改成功！";
            $back['stutas'] = 1;
        }else{
            $back['message'] = "修改失败！";
            $back['stutas'] = 0;
        }
        return $back;
    }
    /**
     * 导航
     * @author yhs 2019.08.01
     * @return back
     */
    public function dataMenu(Request $request){
        $model = Auth::guard('admin')->user();
        if(empty($model)){
            $back['message'] = '请重新登录！';
            $back['status'] = 2;
            return $back;
        }
        $role = Role::where('id',$model->roleid)->first();
        $roleids = explode(',',$role['roleids']);
        $list = Menu::select('id','menu_name','eg_name','parent_id','icon','method')->whereIn('id',$roleids)->where('valid',1)->where('parent_id',0)->get();
        foreach($list as $k => $val){
            if($k == 0){
                $val['index'] = $val['method'];
            }else{
                $val['index'] = (string)$k;
            }
            $val['title'] = $val['menu_name'];
            $subNav = Menu::select('menu_name','eg_name','method')->where('valid',1)->where('parent_id',$val['id'])->get();
            
            if($k != 0){
                foreach($subNav as $key => $value){
                    $value['title'] = $value['menu_name'];
                    $value['index'] = '/'.$value['method'];
                    unset($value['menu_name']);
                    unset($value['eg_name']);
                    unset($value['method']);
                }
                $val['subNav'] = $subNav;
            }
            unset($val['id']);
            unset($val['menu_name']);
            unset($val['parent_id']);
            unset($val['method']);
            unset($val['eg_name']);
        }
        //权限
        $method = (string)trim($request->method,'/');
        $menuid = Menu::where('method',$method)->first();
        if(!empty($menuid)){
            if(!in_array($menuid->id,$roleids)){
                $back['message'] = '没权限，请联系管理员！';
                $back['status'] = 0;
                return $back;
            }
        }
        return $list;
    }
}
